/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package argtrust.apiexamples;


import aima.core.logic.fol.parsing.FOLParser;
import aima.core.logic.fol.domain.FOLDomain;



import argtrust.logic.FOLKnowledgeBase;
import argtrust.logic.InferenceRule;

import argtrust.argsys.ArgumentationSystem;
import argtrust.argsys.ArguingNode;
import argtrust.belief.BeliefBase;
import argtrust.trustnet.Agent;
import argtrust.trustnet.TrustNet;
import argtrust.tidal.TidalTrust;
import argtrust.measure.LevelMeasure;
import argtrust.tidal.TidalTrustBelief;
import argtrust.trustnet.ITrustSemantics;
import argtrust.belief.ITrustBeliefSemantics;
import argtrust.argsys.IBeliefArgumentSemantics;
import argtrust.argsys.MinBeliefArgument;
import argtrust.argsys.IArgumentationSemantics;
import argtrust.argsys.LabelGroundedSemantics;


import argtrust.argtrustgraph.ArgTrustGraph;
/**
 *
 * @author Yuqing Tang
 */
public class JCLExamples {

    public static void main(String[] args) {
        //Create FOL domain:
        FOLDomain domain = new FOLDomain();
        domain.addConstant("hce");
        domain.addConstant("almodovar");
        domain.addPredicate("IndieFilm");
        domain.addPredicate("SpanishFilm");
        domain.addPredicate("DirectedBy");
        domain.addPredicate("Comedy");
        domain.addPredicate("Watch");

        //TrustNetwork:
        TrustNet trust_net = new TrustNet();
        //Agent:
        Agent john = trust_net.getAgent("john");
        Agent alice = trust_net.getAgent("alice");
        Agent dave = trust_net.getAgent("dave");
        Agent jane = trust_net.getAgent("jane");
        Agent mary = trust_net.getAgent("mary");

        trust_net.trust(john, mary, new LevelMeasure(0.9));
        trust_net.trust(mary, jane, new LevelMeasure(0.7));
        trust_net.trust(mary, dave, new LevelMeasure(0.8));
        trust_net.trust(alice, dave, new LevelMeasure(0.4));
        trust_net.trust(alice, jane, new LevelMeasure(0.6));


        //Create FOL sentences and inference rules
        FOLParser fol = new FOLParser(domain);


        //Belief base:
        BeliefBase bb = new BeliefBase();
        bb.believe(john, fol.parse("IndieFilm(hce)"), 1);
        bb.believe(john, fol.parse("SpanishFilm(hce)"), 1);
        bb.believe(john, fol.parse("DirectedBy(hce, almodovar)"), 1);

        bb.believe(john,
                (new InferenceRule()).P_(fol.parse("Comedy(x)")).C_(fol.parse("NOT Watch(x)")),
                1);

        bb.believe(jane,
                (new InferenceRule()).P_(fol.parse("IndieFilm(x)")).P_(fol.parse("SpanishFilm(x)")).C_(fol.parse("NOT Watch(x)")),
                1);

        bb.believe(dave,
                (new InferenceRule()).P_(fol.parse("IndieFilm(x)")).P_(fol.parse("DirectedBy(x, almodovar)")).C_(fol.parse("Watch(x)")),
                1);

        bb.believe(john,
                new InferenceRule().P_(fol.parse("DirectedBy(x, almodovar)")).C_(fol.parse("NOT IndieFilm(x)")),
                1);
//        bb.believe(john,
//                new InferenceRule().P_(fol.parse("NOT IndieFilm(x)")).C_(fol.parse("Watch(x)")),
//                1);
        bb.believe(mary,
                fol.parse("IndieFilm(hce)"),
                1);
//        bb.believe(john, fol.parse("Comedy(hce)"), 1);


        //Argumentation system:
        FOLKnowledgeBase kb = new FOLKnowledgeBase();
        kb.acceptKnowledgeBase(bb.getFactSet(), bb.getRuleSet());
        ArgumentationSystem arg_sys = new ArgumentationSystem(kb);
//        FOLNaturalDeduction deduct_sys = arg_sys.getDeductionSys();
//        InferenceNode inf = deduct_sys.getInferenceNet(fol.parse("NOT Watch(hce)"));
//        while (inf != null) {
//            System.out.println("found: ");
//            inf.debug_print(System.out);
//            inf = deduct_sys.getNextInferenceNet(inf);
//        }
        ArguingNode watch_hce_arggraph
                = arg_sys.getArguingNode(fol.parse("Watch(hce)"));
//        ArguingNode not_watch_hce_arggraph =
//        arg_sys.getArguingNode(fol.parse("NOT Watch(hce)"));

        watch_hce_arggraph.debug_print(System.out);

        ArgTrustGraph gv = new ArgTrustGraph();
        gv.setArgSys(arg_sys);
        gv.setBeliefBase(bb);
        gv.setTrustNet(trust_net);
        TidalTrust trust_sem = new TidalTrust(trust_net, 0);
        gv.setTrustSemantics(trust_sem);

        ITrustBeliefSemantics trust_belief_sem = new TidalTrustBelief(bb, trust_sem);
        gv.setTrustBeliefSemantics(trust_belief_sem);

        IBeliefArgumentSemantics arg_belief_sem = new MinBeliefArgument(trust_belief_sem);
        gv.setArgumentBeliefSemantics(arg_belief_sem);

        IArgumentationSemantics argumentation_sem = new LabelGroundedSemantics(arg_sys, arg_belief_sem);

        gv.setArgumentationSemantics(argumentation_sem);


        gv.openFile("/home/tang/temp/jcl.dot");
        gv.draw(watch_hce_arggraph, trust_net.getTrustNode(john));
//        gv.draw(arg_sys.getAllNodes(), trust_net.getTrustNodes());
        gv.close();

    }
}
